package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.merchant.domain.Product;
import com.ruoyi.merchant.domain.dto.DishDetailUpdateDTO;
import com.ruoyi.merchant.domain.dto.DishSubmitDTO;
import com.ruoyi.merchant.domain.dto.SkuPriceStockUpdateDTO;
import com.ruoyi.merchant.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品管理Controller
 */
@RestController
@RequestMapping("/merchant/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 获取商品详细信息（包含规格）
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return success(productService.selectProductByProductId(productId));
    }

    /**
     * 新增商品并提交审核（包含规格）
     */
    @Log(title = "新增商品提交审核", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    public AjaxResult add(@Validated @RequestBody DishSubmitDTO dishSubmitDTO) {
        int result = productService.insertProductAndSubmitAudit(dishSubmitDTO);
        if (result > 0) {
            return success("商品提交成功，等待平台审核");
        }
        return error("新增商品失败");
    }

    /**
     * 修改商品详情并提交审核（会触发重新审核）
     */
    @Log(title = "修改商品详情提交审核", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/update")
    public AjaxResult updateDetail(@Validated @RequestBody DishDetailUpdateDTO dishDetailUpdateDTO) {
        int result = productService.updateProductDetailAndSubmitAudit(dishDetailUpdateDTO);
        if (result > 0) {
            return success("修改提交成功，等待平台重新审核");
        }
        return error("修改商品失败");
    }

    /**
     * 修改SKU价格和库存（不需要审核）
     */
    @Log(title = "修改SKU价格库存", businessType = BusinessType.UPDATE)
    @PutMapping("/sku/price-stock")
    public AjaxResult updateSkuPriceStock(@Validated @RequestBody SkuPriceStockUpdateDTO skuPriceStockUpdateDTO) {
        return toAjax(productService.updateSkuPriceAndStock(skuPriceStockUpdateDTO));
    }

    /**
     * 删除商品
     */
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return toAjax(productService.deleteProductByProductIds(productIds));
    }

    /**
     * 批量修改商品状态（上下架）
     */
    @Log(title = "商品上下架", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody Map<String, Object> params) {
        Long[] productIds = ((List<Integer>) params.get("productIds"))
                .stream()
                .map(Long::valueOf)
                .toArray(Long[]::new);
        Integer status = (Integer) params.get("status");
        return toAjax(productService.updateProductStatus(productIds, status));
    }

    /**
     * 获取商品统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult statistics() {
        Map<String, Object> stats = productService.selectProductStatistics();
        return success(stats);
    }
}
